package com.shared_file_bucket.controller;


import com.shared_file_bucket.common.domain.R;
import com.shared_file_bucket.common.domain.TreeNode;
import com.shared_file_bucket.common.exception.BusinessException;
import com.shared_file_bucket.common.utils.StringUtils;
import com.shared_file_bucket.model.FileAddDTO;
import com.shared_file_bucket.model.FileOperateDTO;
import com.shared_file_bucket.service.LocalFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/localfile")
@RequiredArgsConstructor
public class LocalFileController {

    private final LocalFileService localFileService;

    /**
     * 获取目录树数据
     *
     * @return
     */
    @GetMapping("/directoryTreeData")
    public R<?> getDirectoryTree(String dir, String shareCode) {
        List<TreeNode<String>> list = localFileService.getSiteDirectoryTreeData(getRoot(shareCode), dir);
        return R.succeed(list);
    }


    /**
     * 查询文件列表
     *
     * @param filePath
     * @param filename
     * @return
     */
    @GetMapping("/list")
    public R<?> getFileList(@RequestParam("filePath") String filePath,
                            @RequestParam("fileName") String filename,
                            String shareCode) {
        return localFileService.getSiteFileList(getRoot(shareCode), filePath, filename);
    }

    /**
     * 新建文件
     *
     * @param dto
     * @return
     * @throws IOException
     */
    @PostMapping("/add")
    public R<?> addFile(FileAddDTO dto,
                        String shareCode) throws IOException {
        localFileService.addFile(getRoot(shareCode), dto);
        return R.succeed("新建成功");
    }

    /**
     * 删除文件
     *
     * @param dtoList
     * @return
     * @throws IOException
     */
    @PostMapping("/delete")
    public R<?> deleteFile(@RequestBody List<FileOperateDTO> dtoList,
                           String shareCode) throws IOException {
        String[] filePathArr = dtoList.stream().map(FileOperateDTO::getFilePath).toArray(String[]::new);
        localFileService.deleteFiles(getRoot(shareCode), filePathArr);
        return R.succeed("删除成功");
    }

    /**
     * 上传文件
     *
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public R<?> uploadFile(@RequestParam("dir") String dir, @RequestParam("file") MultipartFile multipartFile,
                           String shareCode)
            throws IOException {
        if (multipartFile == null) {
            throw new BusinessException("文件不能为空");
        }

        localFileService.uploadFile(getRoot(shareCode), dir, multipartFile);
        return R.succeed("上传成功");
    }

    /**
     * 重命名文件
     *
     * @param dto
     * @return
     * @throws IOException
     */
    @PostMapping("/rename")
    public R<?> renameFile(FileOperateDTO dto,
                           String shareCode) throws IOException {
        localFileService.renameFile(getRoot(shareCode), dto.getFilePath(), dto.getRename());
        return R.succeed("修改成功");
    }

    private String getRoot(String shareCode) {
        String root = null;
        if (StringUtils.isEmpty(shareCode)) {
            root = localFileService.getRootByUserId();
        } else {
            root = localFileService.getRootByShareCode(shareCode);
        }
        return root;
    }
}